import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'

import Components from 'unplugin-vue-components/vite';
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers';

import { svgBuilder } from './src/components/SvgIcon/build';
import VueSetupExtend from 'vite-plugin-vue-setup-extend';
import AutoImport from 'unplugin-auto-import/vite';
import eslint from 'vite-plugin-eslint';

import vueJsx from '@vitejs/plugin-vue-jsx';

// https://vitejs.dev/config/
export default defineConfig({
	/*
	optimizeDeps: {
		include: [
			'dayjs/locale/zh-cn'
		]
	},
	*/

	plugins: [
		vue(),
		Components({
			resolvers: [AntDesignVueResolver({ importStyle: 'less' })],
		}),

		svgBuilder('./src/assets/icon'),

		//<script setup name="">
		VueSetupExtend(),


		AutoImport({
			//默认node_modules/unplugin-auto-import/auto-imports.d.ts
			//通过tds选项修改auto-imports.d.ts生成位置
			//dts: 'src/auto-imports.d.ts',

			//无需导入import {getCurrentInstance} from 'vue';即可直接调用getCurrentInstance();
			imports: ['vue', 'vue-router']
		}),

		eslint({
			include: ['src/**/*.js', 'src/**/*.vue', 'src/*.js', 'src/*.vue'],
		}),

		vueJsx()
	],

	css: {
		preprocessorOptions: {
			less: {
				charset: false,
				modifyVars: {
					'primary-color': 'red',
					hack: 'true; @import "@/styles/theme.less"'
				},
				javascriptEnabled: true
			}

			/*
			样式必须加载less格式，less的样式被css的样式覆盖，会导致自定义样式不生效。
			1. 通过import 'ant-design-vue/dist/antd.css'引入样式，需改成：import 'ant-design-vue/dist/antd.less'；
			2. 通过unplugin-vue-components进行按需加载，需配置：{importStyle: 'less'}
			*/
		},
		devSourcemap: true
	},

	server: {
		port: 3000,
		host: '0.0.0.0',
		https: false,
		cors: true,
		open: true,
		proxy: {
			'/api': {
				target: 'https://crec.njfa.com.cn/api',
				changeOrigin: true,
				rewrite: (path) => path.replace(/^\/api/, '')
			}
		}
	},

	build: {
		target: 'modules',
		outDir: 'dist',

		//指定生成静态资源的存放路径
		assetsDir: 'statics',

		//[vite:terser] terser not found. Since Vite v3, terser has become an optional dependency. You need to install it.
		//yarn add -D terser
		minify: 'terser'
	},

	resolve: {
		alias: {
			"@": path.resolve(__dirname, "./src"),
		},

		//import 'Index' = import 'Index.vue'
		extensions: ['.js', '.vue']
	},
})